package resources;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import users.UnknownUser;
import users.User;

public class SessionDB implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 89L;
	private static final String connection_URL="JDBC:mysql://localhost:3306/VirtualExam";
	public SessionDB(){}
	public List<User> getUsers(){
		List<User> result = new ArrayList<User>();
		try{
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			Properties sessionUser = new Properties();
			sessionUser.put("user", "root");
			sessionUser.put("password", "carcaiac");
			
			Connection con = DriverManager.getConnection(connection_URL, sessionUser);
			try{
				String sql ="SELECT * FROM VirtualExam.Users;";
				PreparedStatement stat = con.prepareStatement(sql);
				ResultSet rs = null;
				stat.execute();
				rs = stat.getResultSet();
				while(rs.next())
				{
					UnknownUser user = new UnknownUser(rs.getString(1),
									rs.getString(2),rs.getString(3),rs.getString(4));
					result.add(user.returnTypeOfUser());
				}
			}finally{con.close();}
			
		}catch(ClassNotFoundException e){e.printStackTrace();}
		catch(IllegalAccessException e){e.printStackTrace();}
		catch(InstantiationException e){e.printStackTrace();}
		catch(SQLException e){e.printStackTrace();}
		
		return result; 
	}

}
